#include"Reactor.hpp"
#include"Connection.hpp"
#include"Listener.hpp"
#include"HandlerConnection.hpp"
#include"InetAddr.hpp"

int main(int argc,char*argv[])
{
    if(argc != 2)
    {
        LOG(DEBUG, "need port!!!\n");
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);
    InetAddr addr("0.0.0.0", 8888);

    //连接管理器
    Listener listener(port);// 专门用来处理新连接到来的模块

    //IO处理器
    HandlerConnection handlers;

    // 事件派发器
    std::unique_ptr<Reactor> reactor = std::make_unique<Reactor>();// 主模块，事件派发

    // 模块之间产生关联
    reactor->SetOnConnect(std::bind(&Listener::Accepter, &listener, std::placeholders::_1));
    reactor->SetNormalHandler(
        std::bind(&HandlerConnection::HandlerRecver,&handlers,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerSender,&handlers,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerExcepter,&handlers,std::placeholders::_1)
    );

    
    reactor->AddConnection(listener.Sockfd(), EPOLLIN|EPOLLET,ListenConnection,addr);

    reactor->Dispatcher();
}